<template>
  <div class="carModels">
    <div class="container">
      <!-- 顶部搜索框 -->
      <div class="top">
        <el-row type="flex" align="middle">
          <span>运输任务编号：</span>
          <el-input
            v-model="seleteDate.transportTaskId"
            placeholder="运输任务编号"
            clearable
            style="width: 200px"
          />
          <span>起始地机构：</span>
          <el-cascader
            v-model="seleteDate.startAgencyId"
            placeholder="请选择起始地机构"
            :options="agencyList"
            :props="defaultProps"
            clearable
            popper-class="theder"
          />
          <span>目的地机构：</span>
          <el-cascader
            v-model="seleteDate.endAgencyId"
            placeholder="请选择目的地机构"
            :options="agencyList"
            :props="defaultProps"
            clearable
            popper-class="theder"
          />
        </el-row>
        <el-row type="flex" align="middle">
          <span>回车时间：</span>
          <el-date-picker
            v-model="value"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
          <span>车辆是否可用：</span>
          <el-select v-model="seleteDate.allowableLoad" placeholder="请选择">
            <el-option
              label="全部"
              value=""
            />
            <el-option
              label="可用"
              value="true"
            />
            <el-option
              label="停用"
              value="false"
            />
          </el-select>
          <el-button
            type="primary"
            @click="btnSearch"
          >搜索</el-button>
          <el-button size="mini" @click="reset">重置</el-button>
        </el-row>
      </div>
    </div>
    <div class="container">
      <div class="table">
        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="transportTaskId" label="运输任务编号" />
          <el-table-column prop="startAgencyName" label="起始地机构" />
          <el-table-column prop="endAgencyName" label="目的地机构" />
          <el-table-column prop="transportOrderNumber" label="运单数量" />
          <el-table-column prop="outStorageTime" label="出车时间" />
          <el-table-column prop="intoStorageTime" label="回车时间" />
          <el-table-column prop="licensePlate" label="车牌号码" />
          <el-table-column prop="isAvailable" label="车辆是否可用" />
          <el-table-column label="操作" fixed="right">
            <template v-slot="{ row }">
              <el-button
                type="text"
                style="color: blue"
                @click="update(row.id)"
              >编辑</el-button>
              <el-button type="text" @cli="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination
          :current-page="tableParams.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="tableParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <div class="container">
      <!-- 放置弹层 -->
    </div>
  </div>
</template>

<script>
import { getCarRegistersList } from '@/api/CarRegister'
import { getOrganizationTreeList } from '@/api/organization'
import { transformData } from '@/utils'

export default {
  data() {
    return {
      total: 1, // 总数据
      // 表格数据请求参数
      tableParams: {
        page: 1,
        pageSize: 10
      },
      seleteDate: {
        intoStorageEndTime: '', // 结束回车时间
        intoStorageStartTime: '', // 开始回车时间
        endAgencyId: null, // 结束机构id
        isAvailable: false, // 车辆是否可用
        startAgencyId: null, // 起始机构id
        transportTaskId: null // 运输任务id
      },
      agencyList: [], // 机构树状信息
      defaultProps: {
        value: 'id',
        children: 'children',
        label: 'name',
        checkStrictly: true
      },
      tableData: [], // 表格数据数组
      value: ''// 回车时间
    }
  },
  watch: {
    // 监听查询的日期输入框收集数据的数组，将数组中的值赋值给对应的变量
    value(newData) {
      console.log(newData)
      if (!newData) {
        // 为解决自定义清空按钮清空后value1变为null而报错的问题
        // 自定义按钮清空的时候 给它设置为 []
        newData = []
        this.seleteDate.intoStorageStartTime = '' // 开始日期
        this.seleteDate.intoStorageEndTime = '' // 结束日期
        return
      }
      this.seleteDate.intoStorageStartTime = this.value1[0]
      this.seleteDate.intoStorageEndTime = this.value1[1]
      // console.log(this.value1)
    }
  },
  created() {
    // 获取回车列表
    // this.getCarRegistersList()
    // 获取树状机构信息
    this.getOrganizationTreeList()
  },
  methods: {
    // 获取表格数据
    async getCarRegistersList() {
      const result = await getCarRegistersList(this.tableParams)
      console.log(result)
      // console.log(result.data.counts)
      this.total = +result.data.counts
      this.tableData = result.data.items
    },
    // 获取树状机构信息
    async getOrganizationTreeList() {
      const res = await getOrganizationTreeList()
      // console.log(res)
      const list = JSON.parse(res.data)
      this.agencyList = await transformData(list)
      // console.log(this.agencyList)
    },
    // 分页器
    handleSizeChange(val) {
      // console.log(val)
      this.tableParams.pageSize = val + ''
      this.getCarRegistersList()
    },
    handleCurrentChange(val) {
      // console.log(val)
      this.tableParams.page = val + ''
      this.getCarRegistersList()
    },
    // 搜索
    async btnSearch() {
      if (this.seleteDate) {
        this.$message.warning('无数据')
        return
      }

      this.tableParams = {
        ...this.tableParams, ...this.seleteDate
      }
      // console.log(this.tableParams)
      await this.getCarRegistersList(this.tableParams)
      this.$message.success('搜索成功')
    },
    // 重置搜索框
    reset() {
      this.seleteDate.intoStorageEndTime = '' // 结束回车时间
      this.seleteDate.intoStorageStartTime = '' // 开始回车时间
      this.seleteDate.endAgencyId = null // 结束机构id
      this.seleteDate.isAvailable = false// 车辆是否可用
      this.seleteDate.startAgencyId = null// 起始机构id
      this.seleteDate.transportTaskId = null // 运输任务id
    },
    // 新增车型
    add() {
      console.log('新增')
    },
    // 删除
    del(id) {
      console.log('删除', id)
    },
    // 编辑
    update(id) {
      console.log('编辑', id)
    }
  }
}
</script>

<style scoped lang="scss">
.carModels {
  background-color: #eee;
  height: 100vh;
  .container {
    margin: 20px auto;
    //版心尺寸
   // width: 1700px;

    .top {
      margin: 10px;
      background-color: #fff;
      padding: 10px;
      span{
         margin:10px;
      }
      .el-input{
        margin:10px;
      }
      .theder {
        .el-cascader-panel {
          .el-cascader-menu {
            .el-radio {
              height: 100%;
              width: 150px;
              opacity: 0;
              position: absolute;
              // z-index: 10;
              .el-radio__input {
                .el-radio__inner {
                  border: 0px;
                }
              }
              .el-radio__input.is-checked {
                .el-radio__inner {
                  background: none;
                }
              }
            }
          }
        }
      }
    }
    .table {
      background-color: #fff;
      padding: 10px;
      margin: 10px;
    }
  }
}
</style>
